home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / misc / dnstools.shar / checkhosts next >
Encoding:
Text File  |  1996-10-25  |  1.7 KB  |  72 lines

  1. #!/usr/local/bin/perl
  2. # program to check /etc/hosts vs. named.hosts
  3. #
  4. # Eric Murray 12/29/93
  5. # ericm@microunity.com
  6.  
  7.  
  8. sub usage {
  9.     print "usage: $0 [-h hosts file] [-n named.hosts file]\n";
  10.     exit 1;
  11. }
  12.  
  13. $hosts = "/etc/hosts";
  14. $named = "/etc/named.d/named.hosts";
  15.  
  16. while ($ARGV[0]) {
  17.     if ($ARGV[0] =~ /^-h/) { shift; $hosts = $ARGV[0]; }
  18.     elsif ($ARGV[0] =~ /^-n/) { shift; $named = $ARGV[0]; }
  19.     elsif ($ARGV[0] =~ /^-i/) {  $iponly++; }
  20.     shift;
  21. }
  22.  
  23. open(FH,"$hosts") || die "can't open $hosts: $!\n";
  24. while(<FH>) {
  25.     chop;
  26.     s/#.*$//;
  27.     next if (/^$/);
  28.     local($ip,$name) = split;
  29.     $hostnames{$ip} = $name;
  30.     $hostips{$name} = $ip if !defined $iponly;
  31. }
  32. close(FH);
  33.  
  34. open(FH,"$named") || die "can't open $named: $!\n";
  35. while(<FH>) {
  36.     chop;
  37.     s/;.*$//;
  38.     next if (/^$/);
  39.     next if ($_ !~ /\s+A\s+\d+/);    # skip all but A records
  40.     local($name,$a,$ip) = split;
  41.     $dnsnames{$ip} = $name;
  42.     $dnsips{$name} = $ip if !defined $iponly;
  43. }
  44. close(<FH>);
  45.  
  46. # now compare & contrast:
  47.  
  48. print "checking $hosts vs. $named:\n";
  49. foreach $ip (sort(keys %hostnames)) {
  50.     if (!defined $dnsnames{$ip}) {
  51.         if (! defined $dnsips{$hostnames{$ip}}) {
  52.             print "$hostnames{$ip} ($ip) in $hosts but not in $named\n"; $bad++;
  53.         }
  54.         else {
  55.          print "IP addr mis-match for $hostnames{$ip}: $hosts says \"$ip\", $named says \"$dnsips{$hostnames{$ip}}\"\n";$bad++;
  56.          }
  57.     }
  58.     elsif ($dnsnames{$ip} ne $hostnames{$ip}) {
  59.         print "hostname mis-match for $ip: hosts says \"$hostnames{$ip}\", $named says \"$dnsnames{$ip}\"\n";$bad++;
  60.         #undef $dnsnames{$ip}; undef $hostnames{$ip};
  61.     }
  62. }
  63.  
  64. print "checking $named vs. $hosts:\n";
  65. foreach $host (sort(keys %dnsips)) {
  66.     if (! defined $hostips{$host}) {
  67.         print "$host in $named but not in $hosts\n"; $bad++;
  68.     }
  69. }
  70.  
  71. exit $bad;
  72.